﻿using System;

namespace UniRx.Operators
{
	// Token: 0x020002B1 RID: 689
	internal class FromEventObservable : OperatorObservableBase<Unit>
	{
		// Token: 0x06000F06 RID: 3846 RVA: 0x0004615A File Offset: 0x0004455A
		public FromEventObservable(Action<Action> addHandler, Action<Action> removeHandler) : base(false)
		{
			this.addHandler = addHandler;
			this.removeHandler = removeHandler;
		}

		// Token: 0x06000F07 RID: 3847 RVA: 0x00046174 File Offset: 0x00044574
		protected override IDisposable SubscribeCore(IObserver<Unit> observer, IDisposable cancel)
		{
			FromEventObservable.FromEvent fromEvent = new FromEventObservable.FromEvent(this, observer);
			return (!fromEvent.Register()) ? Disposable.Empty : fromEvent;
		}

		// Token: 0x040008B7 RID: 2231
		private readonly Action<Action> addHandler;

		// Token: 0x040008B8 RID: 2232
		private readonly Action<Action> removeHandler;

		// Token: 0x020002B2 RID: 690
		private class FromEvent : IDisposable
		{
			// Token: 0x06000F08 RID: 3848 RVA: 0x0004619F File Offset: 0x0004459F
			public FromEvent(FromEventObservable parent, IObserver<Unit> observer)
			{
				this.parent = parent;
				this.observer = observer;
				this.handler = new Action(this.OnNext);
			}

			// Token: 0x06000F09 RID: 3849 RVA: 0x000461C8 File Offset: 0x000445C8
			public bool Register()
			{
				try
				{
					this.parent.addHandler(this.handler);
				}
				catch (Exception error)
				{
					this.observer.OnError(error);
					return false;
				}
				return true;
			}

			// Token: 0x06000F0A RID: 3850 RVA: 0x00046218 File Offset: 0x00044618
			private void OnNext()
			{
				this.observer.OnNext(Unit.Default);
			}

			// Token: 0x06000F0B RID: 3851 RVA: 0x0004622A File Offset: 0x0004462A
			public void Dispose()
			{
				if (this.handler != null)
				{
					this.parent.removeHandler(this.handler);
					this.handler = null;
				}
			}

			// Token: 0x040008B9 RID: 2233
			private readonly FromEventObservable parent;

			// Token: 0x040008BA RID: 2234
			private readonly IObserver<Unit> observer;

			// Token: 0x040008BB RID: 2235
			private Action handler;
		}
	}
}
